package com.itheima.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    MovementService movementService;

    @DubboReference
    CommentService commentService;

    public ResponseEntity findByPage(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findUserInfoById(Long userId) {
        UserInfo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findMovementByCondition4Page(Long userId, Integer state, Integer pageNum, Integer pageSize) {
        //0.若userId不为空,查询userInfo
        UserInfo userInfo = null;
        if (userId != null) {
            userInfo = userInfoService.findById(userId);
        }

        //1.远程调用service获取当前页数据pageBeanVo
        PageBeanVo pageBeanVo = movementService.findByCondition4Page(userId, state, pageNum, pageSize);

        //2.获取动态集合
        List<Movement> items = (List<Movement>) pageBeanVo.getItems();

        //3.遍历动态集合,获取每个动态,获取动态的发布人的userInfo,封装MovementVo,将每个vo放入voList
        List<MovementVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            for (Movement movement : items) {
                if (userInfo == null) {
                    userInfo = userInfoService.findById(movement.getUserId());
                }

                MovementVo vo = new MovementVo();

                vo.setUserInfo(userInfo);
                vo.setMovement(movement);

                //重新设置下时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                voList.add(vo);
            }
        }

        //4.给pageBeanVo设置voList,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMovementById(ObjectId movementId) {
        //查询movement
        Movement movement = movementService.findById(movementId);

        //查询userInfo
        UserInfo userInfo = userInfoService.findById(movement.getUserId());

        //封装vo
        MovementVo vo = new MovementVo();
        vo.setUserInfo(userInfo);
        vo.setMovement(movement);
        //重新设置下时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findMovementComemntByPage(String movementId, Integer pageNum, Integer pageSize) {
        //1.远程调用commentService查询评论信息(pageBeanVo)
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(movementId), pageNum, pageSize, 2);

        //2.在pageBeanVo中获取评论list
        List<Comment> items = (List<Comment>) pageBeanVo.getItems();

        //3.遍历评论list
        List<CommentVo> voList = new ArrayList<>();

        if (CollUtil.isNotEmpty(items)) {
            items.forEach(comment -> {
                //4.获取每条评论的发布人userInfo,封装CommentVo,将每个vo放入voList中
                UserInfo userInfo = userInfoService.findById(comment.getUserId());

                CommentVo vo = new CommentVo();
                vo.setId(comment.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setContent(comment.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                // 将vo对象放入voList中
                voList.add(vo);
            });
        }

        //5.给pageBeanVo设置voList,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
